package com.wangsai.framework.supersecurity.example.cloud.client.controller;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.wangsai.framework.supersecurity.client.OAuth2AccessToken;
import com.wangsai.framework.supersecurity.client.UserStore;
import com.wangsai.framework.supersecurity.example.cloud.commons.MyIUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Mr.Wang
 * @description
 * @date 2021-08-15 09:32
 */
@Api(tags = "授权模块")
@RestController
@RequestMapping
public class AuthController {


    @Autowired
    private UserStore userStore;
    @Autowired
    private SaTokenConfig saTokenConfig;


    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public OAuth2AccessToken login(@RequestParam String username,
                                   @RequestParam String password) {
        MyIUser user = new MyIUser();
        user.setId("110");
        user.setUsername(username);
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        StpUtil.login(user.getId());
        userStore.save(user);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return OAuth2AccessToken.builder().tokenType(saTokenConfig.getTokenPrefix())
                .accessToken(tokenInfo.tokenValue).expiresIn(tokenInfo.getTokenTimeout()).build();

    }
}